<?php

namespace app\index\model;


/**
 * Author:Jack Wang
 * Time:2018-10-29 17:21:25
 * Describe:日志模型
 * Class AccessLog
 * @package app\admin\model
 */
class AccessLog extends Base
{

    //定义只读字段
    protected $readonly = ['access_id'];

    //类型转化
    protected $type = [
        'access_time'    =>  'timestamp',
    ];

    //添加自动字段
    protected $insert = ['ip','access_time','module','controller','action','address','key'];

    //添加自动插入IP
    protected function setIpAttr()
    {
        return request()->ip();
    }

    //添加自动插入访问时间
    protected function setAccessTimeAttr()
    {
        return time();
    }

    //添加自动插入模块
    protected function setModuleAttr()
    {
        return strtolower(request()->module());
    }

    //添加自动插入控制器
    protected function setControllerAttr()
    {
        return strtolower(request()->controller());
    }

    //添加自动插入方法
    protected function setActionAttr()
    {
        return strtolower(request()->action());
    }

    //添加自动插入地址-完整地址包含域名
    protected function setAddressAttr()
    {
        return request()->url(true);
    }


//    //添加自动插入用户id
//    protected function setUserIdAttr()
//    {
//        return (int)$this->user_id;
//    }

    protected function setKeyAttr(){
        return $this->key;
    }

//    //添加自动插入用户名称
//    protected function setUserNameAttr()
//    {
//        return !empty($this->user["user_name"]) ? $this->user["user_name"] : null;
//    }

    //查询设置离开时间
    public function getEndTimeAttr($value){
        return !empty($value) ? date("Y-m-d H:i:s",$value) : 0;
    }


    /**
     * Author:Jack Wang
     * Time:2018-11-01 10:58:28
     * Describe:设置表名-没有表名默认当前月
     * 没有该表创建表
     * @param string $date
     * @return string
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function setTableName($date = ""){
        //没有设置表名的时候默认本月
        $table = $this->prefix."access_log_".(!empty($date) ? $date : date("Y-m"));
        $sql = "CREATE TABLE IF NOT EXISTS `".$table."` (
              `access_id` mediumint(8) NOT NULL AUTO_INCREMENT COMMENT '访问ID',
              `access_time` int(10) NULL DEFAULT NULL COMMENT '访问时间',
              `ip` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'ip地址',
              `module` char(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '模型',
              `controller` char(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '控制器',
              `action` char(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '方法',
              `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '完整地址',
              `key` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户cookie',
              `end_time` int(10) NULL DEFAULT 0 COMMENT '离开时间戳',
              `information_id` mediumint(8) NULL DEFAULT 0 COMMENT '资讯ID',
              `drawing_id` mediumint(8) NULL DEFAULT 0 COMMENT '图纸ID',
              PRIMARY KEY (`access_id`) USING BTREE,
              INDEX `access_time`(`access_time`) USING BTREE,
              INDEX `module`(`module`) USING BTREE,
              INDEX `controller`(`controller`) USING BTREE,
              INDEX `action`(`action`) USING BTREE,
              INDEX `address`(`address`) USING BTREE,
              INDEX `key`(`key`) USING BTREE,
              INDEX `information_id`(`information_id`) USING BTREE,
              INDEX `drawing_id`(`drawing_id`) USING BTREE
            ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;";
        $this->query($sql);
        return $table;
    }

    /**
     * Author:Jack Wang
     * Time:2018-11-01 15:09:29
     * Describe:前台访问日志列表
     * @param $limit
     * @param $page
     * @param string $keyword
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function accessLogList($limit,$page,$keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("address","like","%".$keyword."%")
                        ->whereOr("module","like","%".strtolower($keyword)."%")
                        ->whereOr("controller","like","%".strtolower($keyword)."%")
                        ->whereOr("action","like","%".strtolower($keyword)."%")
                        ->whereOr("ip","like","%".$keyword."%");
                }
            })
            ->order("access_time DESC,access_id DESC")
            ->limit($limit)
            ->page($page)
            ->select();
    }

    /**
     * Author:Jack Wang
     * Time:2018-11-01 15:09:29
     * Describe:前台访问日志列表总数
     * @param string $keyword
     * @return int|string
     */
    public function accessLogListCount($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("address","like","%".$keyword."%")
                        ->whereOr("module","like","%".strtolower($keyword)."%")
                        ->whereOr("controller","like","%".strtolower($keyword)."%")
                        ->whereOr("action","like","%".strtolower($keyword)."%")
                        ->whereOr("ip","like","%".$keyword."%");
                }
            })
            ->count();
    }
}